package com.supor.aiot.md_msg.adapter;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.android.baseconfig.base.GlideApp;
import com.android.baseconfig.base.GlideRequest;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.supor.aiot.common.utils.NotNullUtils;
import com.supor.aiot.md_msg.R;
import com.supor.aiot.md_msg.entity.MsgCenterListBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import androidx.annotation.NonNull;

/**
 * @author: zook
 * @date: 2021/10/14
 */
public class CommunityMsgListAdapter extends BaseQuickAdapter<MsgCenterListBean, BaseViewHolder> {
    public CommunityMsgListAdapter() {
        super(R.layout.item_msg_notic, new ArrayList<>());
        addChildClickViewIds(R.id.ll_content, R.id.ll_right_del);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, MsgCenterListBean item) {
        baseViewHolder.setText(R.id.tv_msg_title, NotNullUtils.getNotNullValue(item.getMessageTitle()))
                .setText(R.id.tv_msg_content, NotNullUtils.getNotNullValue(item.getMessageContent()))
                .setGone(R.id.view_unread, "1".equals(item.getIsRead()))
        ;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            Date date = format.parse(item.getCreateTime());
            baseViewHolder.setText(R.id.tv_msg_date, NotNullUtils.getNotNullValue(format2.format(date)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != item.getAddrJson()) {
            String headUrl = item.getAddrJson().getFaceUrl();
            GlideRequest<Drawable> glideRequest = null;
            if (headUrl == null || headUrl.contains("static/images/user/")) {
                glideRequest = GlideApp.with(getContext())
                        .load(R.mipmap.ic_default_head);
            } else if ("1".equalsIgnoreCase(headUrl)) {
                glideRequest = GlideApp.with(getContext())
                        .load(R.mipmap.ic_default_head1);
            } else if ("2".equalsIgnoreCase(headUrl)) {
                glideRequest = GlideApp.with(getContext())
                        .load(R.mipmap.ic_default_head2);
            } else if ("3".equalsIgnoreCase(headUrl)) {
                glideRequest = GlideApp.with(getContext())
                        .load(R.mipmap.ic_default_head3);
            } else if ("4".equalsIgnoreCase(headUrl)) {
                glideRequest = GlideApp.with(getContext())
                        .load(R.mipmap.ic_default_head4);
            } else {
                glideRequest = GlideApp.with(getContext())
                        .load(item.getAddrJson().getFaceUrl());
            }
            glideRequest
                    .into((ImageView) baseViewHolder.getView(R.id.iv_ava));
        }
    }
}
